﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace 葵花宝典BookDemo.Re
{
    public class MySimpleButton : Button
    {
        public MySimpleButton()
        {
            EventManager.RegisterClassHandler(typeof(MySimpleButton), CustomClickEvent, new RoutedEventHandler(CustomClickClassHandler), false);
        }

        public static readonly RoutedEvent CustomClickEvent = EventManager.RegisterRoutedEvent(
                "CustomClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MySimpleButton));


        // CLR事件的包装器
        public event RoutedEventHandler CustomClick
        {
            add { AddHandler(CustomClickEvent, value); }
            remove { RemoveHandler(CustomClickEvent, value); }
        }

        // 普通CLR事件
        public event EventHandler ClassHandlerProcessed;
        public static void CustomClickClassHandler(object sender, RoutedEventArgs e)
        {
            MySimpleButton simpleBtn = sender as MySimpleButton;
            EventArgs args = new EventArgs();
            simpleBtn.ClassHandlerProcessed(simpleBtn, args);
        }

        // 触发CustomClickEvent
        void RaiseCustomClickEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(MySimpleButton.CustomClickEvent);
            RaiseEvent(newEventArgs);
        }

        // OnClick触发CustomClickEvent
        protected override void OnClick()
        {
            RaiseCustomClickEvent();
        }

    }
}
